package client

import (
	"gorm.io/gorm"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/schemas/resp"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/model/client"
	"likeadmin/util"
	"math"
	"sort"
)

type IRechargeGearService interface {
	List(page request.PageReq, listReq req.RechargeGearListReq) (res response.PageResp, e error)
	Detail(id uint) (res resp.RechargeGearResp, e error)
	Add(addReq req.RechargeGearAddReq) (e error)
	Edit(editReq req.RechargeGearEditReq) (e error)
	Sign() (res []resp.GearSignResp, e error)
	Del(id uint) (e error)
}

// NewRechargeGearService 初始化
func NewRechargeGearService(db *gorm.DB) IRechargeGearService {
	return &rechargeGearService{db: db}
}

// rechargeGearService 充值档位服务实现类
type rechargeGearService struct {
	db *gorm.DB
}

// List 充值档位列表
func (srv rechargeGearService) List(page request.PageReq, listReq req.RechargeGearListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)
	// 查询
	model := srv.db.Model(&client.RechargeGear{})
	if listReq.TId > 0 {
		model = model.Where("t_id = ?", listReq.TId)
	}
	if listReq.Type >= 0 {
		model = model.Where("type = ?", listReq.Type)
	}
	if listReq.Status >= 0 {
		model = model.Where("status = ?", listReq.Status)
	}
	// 总数
	var count int64
	err := model.Count(&count).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	var objs []client.RechargeGear
	err = model.Limit(limit).Offset(offset).Order("id desc").Find(&objs).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}
	resps := []resp.RechargeGearResp{}
	response.Copy(&resps, objs)
	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

// Detail 充值档位详情
func (srv rechargeGearService) Detail(id uint) (res resp.RechargeGearResp, e error) {
	var obj client.RechargeGear
	err := srv.db.Where("id = ?", id).Limit(1).First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}
	response.Copy(&res, obj)
	return
}

// Add 充值档位新增
func (srv rechargeGearService) Add(addReq req.RechargeGearAddReq) (e error) {
	var obj client.RechargeGear
	response.Copy(&obj, addReq)
	obj.Price = uint(math.Round(addReq.Price * 100))
	obj.Buy = obj.Price
	// 会员购买赠送币为0
	if obj.Type == 2 {
		obj.Buy = 0
	}

	obj.TID = addReq.TId
	err := srv.db.Create(&obj).Error
	e = response.CheckErr(err, "Add Create err")
	return
}

// Edit 充值档位编辑
func (srv rechargeGearService) Edit(editReq req.RechargeGearEditReq) (e error) {
	var obj client.RechargeGear
	err := srv.db.Where("id = ?", editReq.Id).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Edit First err"); e != nil {
		return
	}

	obj.Price = uint(math.Round(editReq.Price * 100))
	obj.Buy = obj.Price
	// 会员购买赠送币为0
	if obj.Type == 2 {
		obj.Buy = 0
	}

	err = srv.db.Model(&client.RechargeGear{}).Where("id = ?", editReq.Id).Select("type", "title", "price", "buy", "gift", "vip_days", "sort", "status", "sign", "recharge_num").Updates(client.RechargeGear{
		ID:          editReq.Id,
		Type:        editReq.Type,
		Title:       editReq.Title,
		Price:       obj.Price,
		Buy:         obj.Buy,
		Gift:        editReq.Gift,
		VipDays:     editReq.VipDays,
		Sort:        editReq.Sort,
		Status:      editReq.Status,
		Sign:        editReq.Sign,
		RechargeNum: editReq.RechargeNum,
	}).Error
	e = response.CheckErr(err, "Edit Updates err")
	return
}

// Del 充值档位删除
func (srv rechargeGearService) Del(id uint) (e error) {
	var obj client.RechargeGear
	err := srv.db.Where("id = ?", id).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Del First err"); e != nil {
		return
	}
	// 删除
	err = srv.db.Delete(&obj).Error
	e = response.CheckErr(err, "Del Delete err")
	return
}

func (srv rechargeGearService) Sign() (signs []resp.GearSignResp, e error) {
	signsMap := util.ConfigUtil.GearSigns()

	// 直接构建响应列表
	signs = make([]resp.GearSignResp, 0, len(signsMap))
	for value, label := range signsMap {
		signs = append(signs, resp.GearSignResp{
			Value: value,
			Label: label,
		})
	}

	// 按value排序
	sort.Slice(signs, func(i, j int) bool {
		return signs[i].Value < signs[j].Value
	})

	return signs, nil
}
